package com.yu.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.yu.controller.dto.SelectModel;
import com.yu.entity.StuInfo;
import com.yu.entity.User;
import com.yu.exception.BusinessException;
import com.yu.service.StuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
// TODO 新生信息管理表现层接口
@RestController
@RequestMapping("/stuInfos")
public class StuInfoController {
    @Autowired
    StuInfoService stuInfoService;

    @GetMapping
    public Result selectByPageAndCondition(int currentPage, int pageSize, SelectModel selectModel) {
        System.out.println(selectModel);
        Map<String, Object> map = stuInfoService.selectByPageAndCondition(currentPage, pageSize, selectModel);
        Integer code = map != null ? Code.GET_OK : Code.GET_ERR;
        String msg = map != null ? "" : "数据查询失败，请重试！";
        return new Result(code, map, msg);
    }

    @PostMapping
    public Result save(@RequestBody StuInfo stuInfo) {
        Boolean flag = stuInfoService.save(stuInfo);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        StuInfo stuInfo = stuInfoService.selectById(id);
        Integer code = stuInfo != null ? Code.GET_OK : Code.GET_ERR;
        String msg = stuInfo != null ? "" : "数据查询失败，请重试！";
        return new Result(code, stuInfo, msg);
    }

    @PutMapping
    public Result updateById(@RequestBody StuInfo stuInfo) {
        Boolean flag = stuInfoService.update(stuInfo);
        String msg = flag ? "修改学生信息成功" : "学生信息修改失败，请重试！";
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag, msg);
    }

    @DeleteMapping("/{id}")
    public Result deleteOneById(@PathVariable Integer id) {
        Boolean flag = stuInfoService.deleteOneById(id);
        Integer code = flag ? Code.DELETE_OK : Code.DELETE_ERR;
        String msg = flag ? "数据删除成功" : "数据删除失败，请重试";
        return new Result(code, flag, msg);
    }

    @PostMapping("/batch")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        Boolean flag = stuInfoService.deleteByIds(ids);
        Integer code = flag ? Code.DELETE_OK : Code.DELETE_ERR;
        String msg = flag ? "批量删除数据成功" : "批量删除数据失败，请重试";
        return new Result(code, flag, msg);
    }
    @GetMapping("/export")
    public void exp (HttpServletResponse response) throws IOException {
        List<StuInfo> stuInfos = stuInfoService.selectAllExecptId();
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(stuInfos,true);

        // 设置浏览器写出格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("学生信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out,true);

        out.close();
        writer.close();
    }
    @PostMapping("/import")
    public void imp (MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);

        List<StuInfo> stuInfos = reader.readAll(StuInfo.class);
        Boolean flag = stuInfoService.insertAll(stuInfos);
    }
}
